//
//  PKRegisterViewController.m
//  PianKe
//
//  Created by ybq on 15/12/24.
//  Copyright © 2015年 B7_Young. All rights reserved.
//

#import "PKRegisterViewController.h"
#import "PKRegisterView.h"

@interface PKRegisterViewController () <UITextFieldDelegate>

@property (nonatomic, strong) PKRegisterView *registerView;

@end

@implementation PKRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.hidden = YES;
    [self initUI];
}

- (void)initUI {
    _registerView = [[PKRegisterView alloc] initWithFrame:CGRectMake(0, 20, kWidth, kHeight - 20)];
    [_registerView.backButton addTarget:self action:@selector(backAction:) forControlEvents:(UIControlEventTouchUpInside)];
    _registerView.nickName.textField.delegate = self;
    _registerView.nickName.textField.tag = 100;
    _registerView.mailBox.textField.delegate = self;
    _registerView.mailBox.textField.tag = 200;
    _registerView.passWord.textField.delegate = self;
    _registerView.passWord.textField.secureTextEntry = YES;
    _registerView.passWord.textField.tag = 300;
    [self.view addSubview:_registerView];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
    if (self.view.frame.origin.y != 0) {
        CGRect newFrame = self.view.frame;
        newFrame.origin.y = 0;
        [UIView animateWithDuration:.3 animations:^{
            self.view.frame = newFrame;
        }];
    }
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (textField.tag == 100) {
        CGRect newFrame = self.view.frame;
        newFrame.origin.y = -40;
        [UIView animateWithDuration:.3 animations:^{
            self.view.frame = newFrame;
        }];
    } else if (textField.tag == 200) {
        CGRect newFrame = self.view.frame;
        newFrame.origin.y = -90;
        [UIView animateWithDuration:.3 animations:^{
            self.view.frame = newFrame;
        }];
    } else if (textField.tag == 300) {
        CGRect newFrame = self.view.frame;
        newFrame.origin.y = -120;
        [UIView animateWithDuration:.3 animations:^{
            self.view.frame = newFrame;
        }];
    }
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    if (textField.tag == 100) {
        CGRect newFrame = self.view.frame;
        newFrame.origin.y = 0;
        [UIView animateWithDuration:.5 animations:^{
            self.view.frame = newFrame;
        }];
    } else if (textField.tag == 200) {
        CGRect newFrame = self.view.frame;
        newFrame.origin.y = 0;
        [UIView animateWithDuration:.5 animations:^{
            self.view.frame = newFrame;
        }];
    } else if (textField.tag == 300) {
        CGRect newFrame = self.view.frame;
        newFrame.origin.y = 0;
        [UIView animateWithDuration:.5 animations:^{
            self.view.frame = newFrame;
        }];
    }
    return YES;
}

- (void)backAction:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
